<?php

class Application_Model_Dao_CoursesType extends Zend_Db_Table_Abstract
{

	protected $_name = 'courses_type';
	protected $_primary = 'id';

	public function init(){
			
	}
	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
		throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");
		return $result;
	}

	public function getAllCoursesType(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('courses_type');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	public function getRowCoursesType($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('courses_type')->where('courses_type.id = \''.$id.'\'');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}


	public function getCoursesTypeByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
		throw new Exception("Rodzaj zajęć o ID $id nie istnieje.");
		return $row->toArray();
	}
	public function getCoursesTypeByDesignation($designation){
		$row = $this->fetchRow('designation = '.$designation);
		if (!$row)
		throw new Exception("Rodzaj zajęć o nazwie $designation nie istnieje.");
		return $row->toArray();
	}
	public function saveCoursesType($coursesType) {
		$data = array(
			'designation' => $coursesType->getDesignation(),
			'short' => $coursesType->getShort(),
			'duration_course' => $coursesType->getDurationCourse(),
		);
		$result = $this->insert($data);
		return $result;
	}
	public function updateCoursesType($coursesType) {
		$data = array(
			'designation' => $coursesType->getDesignation(),
			'short' => $coursesType->getShort(),
			'duration_course' => $coursesType->getDurationCourse(),
		);
		$result = $this->update($data, 'id = '. (int)$coursesType->getId());
		return $result;
	}
	public function deleteCoursesType($coursesType) {
		$result = $this->delete('id =' . (int)$coursesType->getId());
		return $result;
	}
	public function getToSelect(){
		$array = $this->getAll()->toArray();
		//$coursesType[''] = "wybierz";
		foreach ($array as $option) {
			$coursesTypes[ $option['id'] ] = $option['designation' ] ;
		}
		return $coursesTypes;
	}
	public function getToSelectClasses(){
		$array = $this->getAll()->toArray();
		$coursesTypes[''] = "wybierz";
		foreach ($array as $option) {
			$coursesTypes[ $option['id'] ] = $option['short'].' '.$option['designation'].' '. $option['duration_course'] ;
		}
		return $coursesTypes;
	}
}
